package vn.csp.gameportal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import vn.csp.congcu.json.JSONParser;
import vn.csp.gameportal.R;
import vn.csp.util.ThongSo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockListFragment;

public class FragmentCategory extends SherlockListFragment{
	private final String TAG= "vn.csp.gameportal.FragmentCategory";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		layDuLieu();
		setRetainInstance(true);
		super.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		View view = inflater.inflate(R.layout.activity_main, container, false);
//		return view;
		return super.onCreateView(inflater, container, savedInstanceState);
	}
	@Override
	  public void onListItemClick(ListView l, View v, int position, long id) {
		TheLoaiGame theLoaiGame = danhSachTheLoaiGame.get(position);
		getActivity().getActionBar().setSubtitle(Html.fromHtml("<font color='#ffffff'>"+theLoaiGame.getTLG_TEN_THE_LOAI()+"</font>"));;
//	      Intent i = new Intent(getActivity().getApplicationContext(), GameDownload.class);
//	      startActivity(i);
 	  }
	/** Danh sách thể loại game ./. */
	List<TheLoaiGame> danhSachTheLoaiGame = new ArrayList<TheLoaiGame>();
	
	/**
	 * Lấy danh sách thể loại game
	 */
    public void layDuLieu() {
		new Thread(){
			@Override
			public void run() {
				JSONParser jsonParser = new JSONParser();
		        try {
		        	ThongSo thongSo = new ThongSo();
		        	JSONObject jsonObject = jsonParser.layDoiTuongJSONTheoYeuCau(thongSo.URL,thongSo.CODE_PHP,ThongSo.YEU_CAU.THE_LOAI_GAME.toString());
		        	JSONArray androidArray = jsonObject.getJSONArray("JSONResponse");
		        	for (int i = 0; i < androidArray.length(); i++) {
		        		danhSachTheLoaiGame.add(new TheLoaiGame(
			    				androidArray.getJSONObject(i).getString("TLG_ID"), 
			    				androidArray.getJSONObject(i).getString("TLG_TEN_THE_LOAI"), 
			    				androidArray.getJSONObject(i).getString("TLG_MO_TA")));
//		        		danh_sach_the_loai_game.add(androidArray.getJSONObject(i).getString("TLG_TEN_THE_LOAI"));
		        		// Lưu danh sách game vì trí trong ListView <=> vị trí trong CSDL (TLG_ID)
//		        		danh_sach_the_loai_game_id.put(i, androidArray.getJSONObject(i).getString("TLG_ID"));
					}
		        	Message m = myHandler.obtainMessage();
					myHandler.sendMessage(m);
				} catch (Exception e) {
					Message m = myHandler.obtainMessage();
					Bundle b = new Bundle();
					b.putString("thong_diep_loi", "Vui lòng kiểm tra kết nối.");
					m.setData(b);
					myHandler.sendMessage(m);
				}
			}
		}.start();
	}
    Handler myHandler = new Handler() {
		public void handleMessage (Message m) {
			String thong_diep_loi = m.getData().getString("thong_diep_loi");
			if(thong_diep_loi!=null){
				Toast.makeText(getActivity().getApplicationContext(),thong_diep_loi, Toast.LENGTH_SHORT).show();
			}else{
				if (!danhSachTheLoaiGame.isEmpty()){
					List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
					//http://wptrafficanalyzer.in/blog/android-listfragment-with-images-and-text-using-android-support-library/
					int count = 0;
					Enumeration<TheLoaiGame>  e = Collections.enumeration(danhSachTheLoaiGame);
					while(e.hasMoreElements()){
						TheLoaiGame tlg = e.nextElement();
			            HashMap<String, String> hm = new HashMap<String,String>();
			            hm.put("the_loai",tlg.getTLG_TEN_THE_LOAI());
			            aList.add(hm);
					  }
			        // Keys used in Hashmap
			        String[] from = {"the_loai"};
			        int[] to = { R.id.ten_game1};
			        SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.categories_listview_layout, from, to);
			        setListAdapter(adapter);
				}
			}
		}
	};
	
}
